package action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.FieldError;
import org.springframework.stereotype.Controller;

import entity.Cusdoc;
import entity.Customer;

import biz.CusdocBiz;
import biz.CustomerBiz;

/**
 * @author 陈彬彬
 * @publish 2015-11-19 下午10:38:49
 * @category 客户文档控制层类
 */
@Controller
public class CusdocAction extends BaseAction {
	@Resource
	private CusdocBiz biz;
	@Resource
	private CustomerBiz cbiz;
	
	private Cusdoc doc;
	private List<Cusdoc> docs;
	private List<Customer> cuss;
	
	private File up;
	private String upFileName;
	private String filename;
	private String upContentType;//文件类型
	
	@Override
	public void addFieldError(String fieldName, String errorMessage) {
		// TODO Auto-generated method stub
		super.addFieldError(fieldName, errorMessage);
		this.getFieldErrors().clear();
		setMsg(errorMessage);
		data();
	}
	
	public String getUpContentType() {
		return upContentType;
	}
	public void setUpContentType(String upContentType) {
		this.upContentType = upContentType;
	}
	public Cusdoc getDoc() {
		return doc;
	}
	public void setDoc(Cusdoc doc) {
		this.doc = doc;
	}
	public List<Cusdoc> getDocs() {
		return docs;
	}
	public void setDocs(List<Cusdoc> docs) {
		this.docs = docs;
	}
	public List<Customer> getCuss() {
		return cuss;
	}
	public void setCuss(List<Customer> cuss) {
		this.cuss = cuss;
	}
	public File getUp() {
		return up;
	}
	public void setUp(File up) {
		this.up = up;
	}
	public String getUpFileName() {
		return upFileName;
	}
	public void setUpFileName(String upFileName) {
		this.upFileName = upFileName;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	public String saveFile(){
		if(up==null)
			return "";
		String pathname=ServletActionContext.getServletContext().getRealPath("/upload");
		File path=new File(pathname);
		if(!path.exists()){
			path.mkdir();
		}
		String fname=new Date().getTime()+"_"+upFileName;
		try {
			FileUtils.copyFile(up, new File(path, fname));
			System.out.println(upContentType);
			return fname;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setMsg("文件上传失败");
		}
		return "文件上传失败";
	}
	
	public String down(){
		return "attr";//进入下载附件视图
	}
	public InputStream getTargetFile(){
		doc=biz.get(getId());
		String fname=doc.getDocname();
		setFilename(fname);
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fname);
	}
	
	public void delFile(String fname){
		String pathname=ServletActionContext.getServletContext().getRealPath("/upload");
		File f=new File(pathname+"/"+fname);
		f.delete();
	}
	
	//增
	public String add(){
		//先执行上传文件，并获取文件名称
		doc.setDocname(saveFile());
		if(biz.add(doc)){
			this.setMsg("添加文档信息成功");		
			doc=null;
		}else{
			this.setMsg("添加文档信息失败");		
		}
		return data();
	}
	//删
	public String del(){
		doc=biz.get(getId());
		String fname=doc.getDocname();
		if(biz.del(getId())){
			this.setMsg("删除文档信息成功！");
			//如果数据库删除数据成功，则需要删除该文档信息对应的文件
			delFile(fname);
		}else{
			this.setMsg("删除文档信息失败！");
		}
		return list();
	}
	//改
	public String update(){
		if(up!=null){
			//先删除旧文件，再执行上传文件，并获取文件名称
			delFile(doc.getDocname());
			doc.setDocname(saveFile());
		} 
		if(biz.update(doc)){
			this.setMsg("更新文档信息成功！");
			return list();
		}else{
			this.setMsg("更新文档信息失败！");
			return data();
		}
	}
	//查
	public String list(){
		int empid=getCurUser().getEmpid();
		docs=biz.queryByPage(getPage(), 5,empid);
		int count=biz.count(empid);
		setTpage(count%5==0?count/5:(count/5+1));
		return "list";
	}
	
	public String data(){
		cuss=cbiz.queryByPage(1, 90, getCurUser().getEmpid());
		return "edit";
	}
	public String show(){
		doc=biz.get(getId());
		if(doc==null){
			setMsg("该文档信息不存在！");
			return list();
		}
		return data();
	}
}
